<template>
  <div class="page" v-loading="loading">
    <div style="margin-bottom: 10px;">
      <el-input placeholder="请输入分类名称" v-model='categoryName' style="width:200px"></el-input>
      <el-button type="primary" @click="addCategory(0, categoryName)">新增分类</el-button>
    </div>
    <div style="margin-bottom: 15px;">
      <el-table :data="categoryList" style="width: 100%;margin-bottom: 20px;" row-key="id" border default-expand-all :tree-props="{children: 'subCategoryList'}">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="唯一编号" prop="id"></el-table-column>
        <el-table-column label="分类名称">
          <template slot-scope="scope">
            <div v-if="scope.row.flag">
               <el-input v-model="scope.row.typeName"></el-input>
            </div>
            <span v-else>{{scope.row.typeName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="!scope.row.isDel" size="mini" type="success">正常</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <div>
              <el-button size="mini" type="primary" v-if="scope.row.parentId == 0" @click="form.parentData = scope.row; dialogVisible = true">
                <i class="el-icon-plus"></i>新增子类
              </el-button>
              <el-button type="primary" size="mini" @click="openUpdateFlag(scope.row, scope.row.flag)">
                <span v-if="!scope.row.flag"><i class="el-icon-edit"></i> 修改</span>
                <span v-else><i class="el-icon-circle-check"></i> 保存</span>
              </el-button>
              <el-button v-if="scope.row.flag" @click="scope.row.flag = false;" type="info" size="mini"><i class="el-icon-warning-outline"></i>取消</el-button>
              <el-button type="danger" size="mini" v-if="!scope.row.subCategoryList" @click="del(scope.row.id)"><i class="el-icon-delete"></i> 删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="新建子分类" :visible.sync="dialogVisible" width="30%" center >
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="父分类名称:" >
          <el-input v-model="form.parentData.typeName" disabled></el-input>
        </el-form-item>
        <el-form-item label="当前分类名称:">
          <el-input v-model="form.cateName" placeholder="请输入子分类名称"></el-input>
        </el-form-item>
        <el-form-item style="padding-left: 30px;">
          <el-button @click="dialogVisible = false" type="info">取 消</el-button>
          <el-button type="primary" @click="addCategory(form.parentData.id, form.cateName)">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "manage-category",
    data() {
      return {
        categoryList: [],
        loading: false,
        categoryName:'',
        dialogVisible:false,
        form:{
          parentData:{},
          cateName:'',
        }
      }
    },
    methods: {
      initPage() {
        this.categoryList = [];
        this.loading = true
        const thizz = this;
        setTimeout(_ => {
          thizz.$get("/category/list").then(res => {
            if (res.code == 200) {
              thizz.categoryList = this.initItem(res.data);
            }
            thizz.loading = false
          })
        }, 300)
      },
      initItem(data){
        data.forEach(item => {
           item.flag = false;
           let sub = item.subCategoryList;
           if(sub != null && sub.length != 0) sub.forEach(s => s.flag = false);
        })
        return data;
      },
      openUpdateFlag(row, save) {
        if(save){
            this.$post("/category/createOrUpdate", {id: row.id, typeName: row.typeName}).then(result => {
              if(result.code == 200){
                 this.$message.success(result.data);
                this.initPage();
              }else
                this.$message.warning(result.data);
            })
        }
        this.initItem(this.categoryList);
        if(!save) row.flag = true;
      },
      addCategory(parentId, categoryName){
        if(categoryName.length == 0 || categoryName == '') {
          return this.$message.warning("请输入分类名称");
        }
        this.$post("/category/createOrUpdate", {parentId: parentId, typeName: categoryName}).then(result => {
          if(result.code == 200){
            this.$message.success(result.data);
            this.initPage();
          }else
            this.$message.warning(result.data);
          this.dialogVisible = false;
        })
      },
      del(id){
        let ids = [id];
        this.$del("/category/delete", {ids: ids.toString()}).then(result => {
          if(result.code == 200){
             this.$message.success("删除成功条数:" + result.data);
             this.initPage();
          }else
            this.$message.warning("删除失败");
        })
      }
    },
    created() {
      this.initPage()
    }
  }
</script>

<style lang="scss" scoped>
  .page {
    border-radius: 8px;
    background-color: #fff;
    min-height: calc(100vh - 105px);
    box-sizing: border-box;
    padding: 15px;
  }
</style>
